package com.lw.lost.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lw.lost.R
import com.lw.lost.databinding.ItemMomentBinding
import com.lw.lost.db.DBlost
import com.lw.lost.models.Moment
import java.io.File

class MomentAdapter(
    private val onLikeClick: (Moment) -> Unit,
    private val onCommentClick: (Moment) -> Unit,
    private val currentUserId: Long,
    private val db: DBlost
) : ListAdapter<Moment, MomentAdapter.ViewHolder>(DiffCallback()) {

    inner class ViewHolder(private val binding: ItemMomentBinding) : RecyclerView.ViewHolder(binding.root) {
        private val commentAdapter = CommentAdapter()

        fun bind(moment: Moment) {
            binding.apply {
                // 设置用户信息
                tvUsername.text = moment.userName
                tvTime.text = moment.createTime

                // 加载用户头像
                Glide.with(ivAvatar)
                    .load(moment.userAvatar)
                    .circleCrop()
                    .placeholder(R.drawable.ic_avatar_default)
                    .error(R.drawable.ic_avatar_default)
                    .into(ivAvatar)

                // 设置内容
                if (moment.content.isNotEmpty()) {
                    tvContent.visibility = View.VISIBLE
                    tvContent.text = moment.content
                } else {
                    tvContent.visibility = View.GONE
                }

                // 设置图片
                if (!moment.imageUrl.isNullOrEmpty()) {
                    ivImage.visibility = View.VISIBLE
                    Glide.with(ivImage)
                        .load(File(moment.imageUrl))
                        .into(ivImage)
                } else {
                    ivImage.visibility = View.GONE
                }

                // 设置点赞状态
                val isLiked = db.postDao.isLikedByUser(moment.postId, currentUserId)
                btnLike.isSelected = isLiked
                
                // 设置点赞数
                tvLikeCount.text = moment.likeCount.toString()
                tvLikeCount.setTextColor(
                    ContextCompat.getColor(
                        tvLikeCount.context,
                        if (isLiked) R.color.red else R.color.text_color
                    )
                )

                // 点赞按钮点击事件
                btnLike.setOnClickListener {
                    onLikeClick(moment)
                }

                // 设置评论按钮点击事件
                btnComment.setOnClickListener {
                    // 切换评论区域的显示/隐藏
                    if (commentLayout.visibility == View.VISIBLE) {
                        commentLayout.visibility = View.GONE
                    } else {
                        commentLayout.visibility = View.VISIBLE
                        // 加载评论列表
                        val comments = db.postDao.getComments(moment.postId)
                        commentAdapter.submitList(comments)
                    }
                    onCommentClick(moment)
                }

                // 设置评论列表
                rvComments.apply {
                    layoutManager = LinearLayoutManager(context)
                    adapter = commentAdapter
                }

                // 设置发送评论按钮点击事件
                btnSendComment.setOnClickListener {
                    val content = etComment.text.toString().trim()
                    if (content.isNotEmpty()) {
                        if (db.postDao.addComment(moment.postId, currentUserId, content)) {
                            // 评论成功，清空输入框并刷新评论列表
                            etComment.text.clear()
                            val comments = db.postDao.getComments(moment.postId)
                            commentAdapter.submitList(comments)
                            // 更新评论数显示
                            tvCommentCount.text = comments.size.toString()
                        }
                    }
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemMomentBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    private class DiffCallback : DiffUtil.ItemCallback<Moment>() {
        override fun areItemsTheSame(oldItem: Moment, newItem: Moment): Boolean {
            return oldItem.postId == newItem.postId
        }

        override fun areContentsTheSame(oldItem: Moment, newItem: Moment): Boolean {
            return oldItem == newItem
        }
    }
}